<template>
	<view class="user">
		<view class="header">
			<u-sticky offset-top="0" h5-nav-height="0" bg-color="transparent">
				<u-navbar :is-back="false" title="个人中心" :title-bold="true" :is-fixed="false" :border-bottom="false"
					:background="{ background: 'rgba(256,256, 256,' + navBg + ')' }"
					:title-color="navBg > 0.5 ? '#000' : '#fff'"></u-navbar>
			</u-sticky>
			<view class="user-info row-between  bg-white">
				<view class="info row">
					<image class="avatar mr20 flexnone" @tap="goLogin"
						:src="isLogin ? userInfo.avatar : 'https://image.leyuanyinyong.cn/uploads/images/20210810215837150223769.png'"></image>
					<view v-if="isLogin">
						<view class="name xxl line1">{{userInfo.nickname}}</view>
						<view class="user-level row">
							<view class="xs white vip" v-if="false">
								<u-icon name="level" color="#ffba40"></u-icon>
							</view> 
							<view class="xs normal row-center user_sn muted" v-html="'编号:' + userInfo.sn"></view>
							<view class="xs normal row-center user_level " v-text="userInfo.level"></view>
						</view>
					</view>
					<view v-else @tap="goLogin">
						<view style="font-size: 38rpx">点击登录</view>
						<view class="sm">登录体验更多功能</view>
					</view>
				</view>
				<view class="row" style="align-self: flex-start;">
					<view class="user-inforamtion-update-button" v-if="isLogin == true"
						@tap="goPage('/pages/bundle/user_profile/user_profile')">
						修改资料
						<u-icon name="arrow-right" style="margin: 0 10rpx;"></u-icon>
					</view>
					<view class="user-opt" v-if="false" @tap="goPage('/pages/bundle/user_profile/user_profile')">
						<u-icon :size="38" label-color="#000" name="setting-fill"></u-icon>
					</view>
				</view>
			</view>
			<view class="my-assets bg-white">
				<view class="nav row">
					<view class="column-center mb20 assets-item" @tap="goPage('/pages/bundle/user_wallet/user_wallet')">
						<view class="xxl bold">{{userInfo.user_money}}</view>
						<view class="sm">余额</view>
					</view>
					<view class="column-center mb20 assets-item" @tap="goPage('/pages/user_coupon/user_coupon')">
						<view class="xxl bold">{{userInfo.coupon}}张</view>
						<view class="sm">优惠券</view>
					</view>
					<!-- <view class="column-center mb20 assets-item" @tap="goPage('/pages/bundle/user_sign/user_sign')"> -->
					<view class="column-center mb20 assets-item" @tap="$toast({'title':'正在开发中'})">
						<view class="xxl bold">{{userInfo.user_shopping_gold}}</view>
						<view class="sm">福利金</view>
					</view>
				</view>
			</view>
		</view>

		<view class="order-nav bg-white">
			<view class="title row-between" @tap="goPage('/pages/user_order/user_order')">
				<view class="lg bold">我的订单</view>
				<view class="muted sm row">
					全部
					<image class="icon-sm ml10" src="/static/images/arrow_right.png"></image>
				</view>
			</view>
			<view class="nav row">
				<view class="item column-center mb20" @tap="goPage('/pages/user_order/user_order?type=pay')">
					<view class="icon-contain">
						<view v-if="userInfo.wait_pay" class="badge xs row-center bg-white">
							{{userInfo.wait_pay}}
						</view>
						<image class="nav-icon" src="https://image.leyuanyinyong.cn/uploads/images/202108102159468bbfa8035.png"></image>
					</view>
					<view class="sm mt10">待付款</view>
				</view>
				<view class="item column-center mb20" @tap="goPage('/pages/user_order/user_order?type=delivery')">
					<view class="icon-contain">
						<view v-if="userInfo.wait_delivery" class="badge xs row-center bg-white">
							{{userInfo.wait_delivery}}
						</view>
						<image class="nav-icon mb10" src="https://image.leyuanyinyong.cn/uploads/images/20210810220012d1fbd0997.png"></image>
					</view>
					<view class="sm">待发货</view>
				</view>
				<view class="item column-center mb20" @tap="goPage('/pages/user_order/user_order?type=delivery')">
					<view class="icon-contain">
						<view v-if="userInfo.wait_take" class="badge xs row-center bg-white">
							{{userInfo.wait_take}}
						</view>
						<image class="nav-icon" src="https://image.leyuanyinyong.cn/uploads/images/2021081022003468ac07664.png"></image>
					</view>
					<view class="sm mt10">待收货</view>
				</view>
				<view class="item column-center mb20"
					@tap="goPage('/pages/bundle/goods_comment_list/goods_comment_list')">
					<view class="icon-contain">
						<view v-if="userInfo.wait_comment" class="badge xs row-center bg-white">
							{{userInfo.wait_comment}}
						</view>
						<image class="nav-icon" src="https://image.leyuanyinyong.cn/uploads/images/202108102200564860c3590.png"></image>
					</view>
					<view class="sm mt10">商品评价</view>
				</view>
				<view class="item column-center mb20" @tap="goPage('/pages/bundle/post_sale/post_sale')">
					<view class="icon-contain">
						<view v-if="userInfo.after_sale" class="badge xs row-center bg-white">
							{{userInfo.after_sale}}
						</view>
						<image class="nav-icon" src="https://image.leyuanyinyong.cn/uploads/images/20210810220118c28fa1733.png"></image>
					</view>
					<view class="sm mt10">退款/售后</view>
				</view>
			</view>
		</view>
		<view class="server-nav bg-white" v-if="menuList && menuList.length > 0">
			<view>
				<view class="title row-between">
					<view class="lg bold">常用功能</view>
				</view>
			</view>
			<view class="nav row wrap">
				<button v-for="(item, index) in menuList" :key="index" class="item column-center mb20"
					hover-class="none" :open-type="item.link_type == 3 ?'contact' :''" @tap="tapMenu(item)"
					style="width: 25%;">
					<image class="nav-icon" :src="item.image"></image>
					<text class="sm">{{item.name}}</text>
				</button>
			</view>
		</view>
		<u-button v-if="isLogin" :custom-style="userPageButton" @click="logout" :hair-line="false">退出登录</u-button>
		<tabbar></tabbar>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapActions
	} from 'vuex'
	import {
		userLogout,
		getUser
	} from '@/api/user';
	import {
		getMenu
	} from '@/api/store'
	import {
		toLogin,
		wxMnpLogin
	} from '@/utils/login'
	import {
		menuJump,
		copy,
		setTabbar
	} from '@/utils/tools'
	import Cache from '@/utils/cache'
	const app = getApp()
	export default {
		data() {
			return {
				showNav: false,
				navH: 0,
				navBg: 0,
				menuList: [],
				statusBarH: "",
				userPageButton: {
					width: '95%',
					margin: '20rpx auto 0',
					border: 'none',
					color: '#000000',
					height: '90rpx',
					lineHeight: '90rpx',
					fontWeight: 'bold'
				}
			};
		},

		components: {},
		props: {},

		onLoad(options) {
			setTabbar()
			this.getMenuFun();
		},

		onShow() {
			this.getUser();
			this.getCartNum()
		},
		onPageScroll(e) {
			const top = uni.upx2px(100)
			const {
				scrollTop
			} = e
			let percent = scrollTop / top > 1 ? 1 : scrollTop / top
			this.navBg = percent
		},

		onUnload() {},
		onPullDownRefresh() {
			this.getUser().then(() => {
				uni.stopPullDownRefresh();
			})
			this.getMenuFun();
		},
		onShareAppMessage() {
			const shareInfo = Cache.get('shareInfo')
			return {
				title: shareInfo.mnp_share_title,
				path: "pages/index/index?invite_code=" + this.inviteCode
			};
		},
		methods: {
			...mapActions(['getCartNum', 'getUser']),
			goLogin() {
				let {
					isLogin
				} = this;
				if (isLogin) {
					uni.navigateTo({
						url: '/pages/bundle/user_profile/user_profile'
					});
					return;
				}
				toLogin()
			},

			goPage(url) {
				if (!this.isLogin) return toLogin()
				uni.navigateTo({
					url,
					fail(e) {
						console.log(e)
					}
				});
			},
			tapMenu(item) {
				if (!this.isLogin) return toLogin()
				menuJump(item)
			},
			async getMenuFun() {
				const {
					data,
					code
				} = await getMenu({
					type: 2,
				})
				if (code == 1) {
					this.menuList = data
				}
			},

			onCopy(e) {
				copy(this.userInfo.sn)
			},
			
			logout() {
				//  退出登录
				userLogout({
					token: this.token
				}).then(res => {
					if (res.code == 1) {
						this.$store.commit("LOGOUT");
						this.$toast({
							title: '登出成功'
						})
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/index/index"
							})
						}, 500)
					}
				})
			}
		},
		computed: {
			...mapGetters(["cartNum", "userInfo", "inviteCode", "appConfig"])
		}
	};
</script>
<style lang="scss">
	.user {
		background-image: url($u-resource-imgaes-url + 'user/header-bg.png');
		background-size: 100% 320rpx;
		background-repeat: no-repeat;

		.header {
			display: flex;
			flex-direction: column;

			// height: 420rpx;
			.user-info {
				padding: 30rpx 30rpx;
				margin: 0 auto;
				width: 95%;
				border-radius: 20rpx 20rpx 0 0;
				position: relative;

				.info {
					width: 70%;

					.avatar {
						height: 110rpx;
						width: 110rpx;
						border-radius: 50%;
						overflow: hidden;
					}
				}

				.row {
					.user-inforamtion-update-button {
						position: absolute;
						bottom: 30rpx;
						right: 0;
						background: #fd5358;
						padding: 6rpx 0 6rpx 20rpx;
						border-radius: 100rpx 0 0 100rpx;
						color: #fff;
					}
				}

				.name {
					text-align: left;
					margin-bottom: 5rpx;
					max-width: 400rpx;
				}

				.user-level {
					.vip {
						background-color: #2e2e2e;
						height: 36rpx;
						line-height: 40rpx;
						width: 36rpx;
						text-align: center;
						border-radius: 8rpx;
					}

					.user_level {
						background-color: #daa265;
						color: #FFFFFF;
						padding: 2rpx 16rpx;
						border-radius: 10rpx;
						margin-left: 10rpx;
					}
				}

				.user-opt {
					position: relative;

					.dot {
						position: absolute;
						background-color: #ee0a24;
						border: 2rpx solid #FFFFFF;
						// color: $-color-primary;
						border-radius: 100%;
						top: 0;
						right: -4rpx;
						// font-size: 22rpx;
						min-width: 16rpx;
						height: 16rpx;
					}
				}

				.buyer-type {
					background-color: #FFA200;
					height: 38rpx;
					padding: 0 10rpx;
				}
			}

			.member {
				flex: 1;
				padding: 0 20rpx;

				.member-entery {
					color: #FFE0A1;
					padding: 20rpx 30rpx 0;
					width: 100%;
					height: 90rpx;
					background: url('https://image.leyuanyinyong.cn/uploads/images/202108102202110a2b57206.png');
					background-size: 100%;
					margin-top: -10px;
				}
			}

		}

		.order-nav,
		.my-assets {
			// margin: 20rpx 20rpx 0;
			width: 95%;
			margin: 0 auto;
			border-radius: 0 0 20rpx 20rpx;
			z-index: 1;
		}

		.order-nav {
			margin-top: 20rpx;
			border-radius: 20rpx;

			.icon-contain {
				position: relative;
			}
		}



		.server-nav {
			margin: 20rpx 20rpx 0;
			border-radius: 20rpx;
		}

		.title {
			height: 88rpx;
			padding: 0 30rpx;
			// border-bottom: $-dashed-border;
		}

		.nav {
			padding: 26rpx 0 0;

			.assets-item {
				flex: 1;
			}

			.item {
				width: 25%;
			}

			.badge {
				padding: 0 6rpx;
				min-width: 28rpx;
				height: 28rpx;
				border-radius: 28rpx;
				box-sizing: border-box;
				border: 1rpx solid #fa3029;
				background-color: #fa3029;
				color: #fff;
				position: absolute;
				left: 40rpx;
				top: 0rpx;
				z-index: 2;
			}

			.nav-icon {
				// width: 52rpx;
				width: 50rpx;
				// height: 52rpx;
				height: 50rpx;
			}
		}
	}
</style>
